/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web;

import com.representqueens.spark.LineGraph;
import com.representqueens.spark.SizeParams;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.xptracker.services.reports.ReportFactory;
import org.xptracker.services.reports.StoryHistory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.SortedMap;

@UrlBinding("/EffortSpark.action")
public class EffortSparkActionBean extends StandardActionBean {
  private String storyId;

  public String getStoryId() {
    return storyId;
  }

  public void setStoryId(String storyId) {
    this.storyId = storyId;
  }

  public Resolution createSparkline() {
    SortedMap<Date, Long> effortMap = ReportFactory.getInstance().makeStoryHistory(StoryHistory.EFFORT, storyId).getTimeline();
    SortedMap<Date, Double> estimateMap = ReportFactory.getInstance().makeStoryHistory(StoryHistory.ESTIMATES, storyId).getTimeline();

    List<Double> datapoints = new ArrayList<Double>();
    boolean estimateExists = false;
    for(Date d: effortMap.keySet()) {
      double effort = effortMap.get(d) == null ? 0.0 : effortMap.get(d);
      effort = effort / 1000 / 60 / 60;

      double estimate = estimateMap.get(d) == null ? 0.0 : estimateMap.get(d);
      if (!estimateExists && estimate > 0) {
        estimateExists = true;
      }

      double value = estimate == 0 ? 0 : (estimate - effort) / estimate;
      if (estimateExists) {
        datapoints.add(value <= 0 ? 0.0 : value * 100);
      }
    }

    try {
      BufferedImage i = LineGraph.createGraph(datapoints.toArray(new Double[0]), new SizeParams(50, 14, 1, 0f, 100f),
          Color.blue.darker());
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      ImageIO.write(i, "png", out);
      return new StreamingResolution("image/png", new ByteArrayInputStream(out.toByteArray()));
    } catch (IOException e) {
      return new StreamingResolution("text/plain", new ByteArrayInputStream("Error generating chart".getBytes()));
    }
  }
}
